<template>
  <div>
    <h1>{{ id ? "编辑" : "新建" }}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs value="three">
        <el-tab-pane label="基础信息" name="first">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="(res) => this.$set(this.model, 'avatar', res.url)"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="皮肤" class="banner">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="(res) => this.$set(this.model, 'banner', res.url)"
            >
              <img v-if="model.banner" :src="model.banner" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <el-form-item label="分类">
            <el-select v-model="model.categories" multiple>
              <el-option
                v-for="item in categories"
                :label="item.name"
                :value="item._id"
                :key="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate
              v-model="model.scores.difficult"
              :max="9"
              show-score
              style="margin-top: 0.6rem"
            ></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate
              v-model="model.scores.skills"
              :max="9"
              show-score
              style="margin-top: 0.6rem"
            ></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate
              v-model="model.scores.attack"
              :max="9"
              show-score
              style="margin-top: 0.6rem"
            ></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate
              v-model="model.scores.survive"
              :max="9"
              show-score
              style="margin-top: 0.6rem"
            ></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option
                v-for="item of items"
                :label="item.name"
                :value="item._id"
                :key="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option
                v-for="item of items"
                :label="item.name"
                :value="item._id"
                :key="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能" name="second">
          <el-button
            size="small"
            type="primary"
            @click="model.skills.unshift({})"
            icon="el-icon-plus"
            >添加技能</el-button
          >
          <el-row>
            <el-col :md="12" v-for="(item, index) in model.skills" :key="index">
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>

              <el-form-item label="冷却值">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="danger"
                  round
                  size="mini"
                  @click="model.skills.splice(index, 1)"
                  icon="el-icon-delete"
                ></el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      <el-tab-pane label="最佳搭档" name="three">
          <el-button
            size="small"
            type="primary"
            @click="model.partners.unshift({})"
            icon="el-icon-plus"
            >添加英雄</el-button
          >
          <el-row>
            <el-col :md="12" v-for="(item, index) in model.partners" :key="index">
              <el-form-item label="英雄">
                <el-select
                  filterable
                  v-model="item.hero">
                  <el-option
                    v-for="hero in heroes"
                    :key="hero._id"
                    :value="hero._id"
                    :label='hero.name'
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="danger"
                  round
                  size="mini"
                  @click="model.partners.splice(index, 1)"
                  icon="el-icon-delete"
                ></el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" native-type="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  props: {
    id: {}
  },
  data () {
    return {
      categories: [],
      items: [],
      heroes: [],
      model: {
        avatar: '',
        categories: '',
        skills: [],
        scores: {
          difficult: 0
        },
        partners: [],
        items1: [],
        items2: []
      }
    }
  },
  methods: {
    // 保存英雄
    async save () {
      if (!this.model.name) return this.$message({ type: 'error', message: '请填写数据' })

      if (this.id) {
        // 如果传入了id就发送编辑英雄名称的请求
        await this.$http.put(`rest/heroes/${this.id}`, this.model)
      } else {
        // 没有id就是发送创建英雄名称
        await this.$http.post('rest/heroes', this.model)
      }
      // this.$router.push('/heroes/list')
      this.$message({
        type: 'success',
        message: '保存成功'
      })
    },
    // 编辑英雄
    async fetch () {
      const res = await this.$http.get(`rest/heroes/${this.id}`)
      this.model = { ...this.model, ...res.data }
    },
    // 图片上传成功之后
    afterUpload (res, file) {
      // this.model.avatar = res.url
      this.$set(this.model, 'avatar', res.url)
    },
    // 英雄分类选择
    async fetchCategories () {
      const res = await this.$http.get('rest/categories')
      // eslint-disable-next-line vue/no-mutating-props
      this.categories = res.data
    },
    // 装备选择
    async fetchItems () {
      const res = await this.$http.get('rest/items')
      this.items = res.data
    },
    // 选择英雄
    async fetchHero () {
      const res = await this.$http.get('rest/heroes')
      this.heroes = res.data
    }
  },
  created () {
    this.id && this.fetch()
    this.fetchCategories()
    this.fetchItems()
    this.fetchHero()
  }
}
</script>

<style scoped>
.banner >>> .avatar {
  width: auto;
}
</style>
